﻿using System.Runtime.Caching;
using System;
using System.Collections.Generic;
using Microsoft.Extensions.Caching.Memory;
using System.Linq;
using System.ComponentModel;
using System.Net;
using System.Diagnostics;

namespace TestProject
{
    class Program
    {
        static void Main(string[] args)
        {
            var oldUser = new User
            {
                Id = "1",
                Age = "21",
                Gender = "女",
                Name = "李四"
            };
            var newUser = new User
            {
                Id = "1",
                Age = "21",
                Gender = "女",
                Name = "丽丽"
            };
            CompareTwoEntity(oldUser, newUser);
            Console.ReadKey();
        }

        public static void CompareTwoEntity<T>(T oldModel, T newModel)
        {
            //用来存放比较后的结果
            var entity = new CompareJson();
            Type typeDescription = typeof(DescriptionAttribute);
            if (oldModel != null && newModel != null)
            {
                //反射获取类（泛型）的所有属性信息
                System.Reflection.PropertyInfo[] mPi = typeof(T).GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
                var oldData = new List<OldData>();
                var newData = new List<NewData>();
                //循环获取每一个属性的信息
                for (int i = 0; i < mPi.Length; i++)
                {
                    System.Reflection.PropertyInfo pi = mPi[i];
                    object[] arr = pi.GetCustomAttributes(typeDescription, true);
                    //获取类的属性
                    string atrr = arr.Length > 0 ? ((DescriptionAttribute)arr[0]).Description : pi.Name;

                    //获取属性值
                    object oldObj = pi.GetValue(oldModel, null);
                    object newObj = pi.GetValue(newModel, null);
                    string oldValue = oldObj == null ? "" : oldObj.ToString();
                    string newValue = newObj == null ? "" : newObj.ToString();
                    //判断值是否相同，不相同则把旧值和新值都保存起来。
                    if (oldValue != newValue)
                    {
                        oldData.Add(new OldData {  field = atrr,value=oldValue});
                        newData.Add(new NewData {  field = atrr,value=newValue});
                    }
                }
                entity.oldData = oldData;
                entity.newData = newData;
                var json = Newtonsoft.Json.JsonConvert.SerializeObject(entity);
                Console.WriteLine(json);
            }
        }

    }
    class User
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string Gender { get; set; }
        public string Age { get; set; }
    }
    class CompareJson
    {
        public List<OldData> oldData { get; set; }
        public List<NewData> newData { get; set; }
    }
    class OldData
    {
        public string field { get; set; }
        public string value { get; set; }
    }
    class NewData
    {
        public string field { get; set; }
        public string value { get; set; }
    }


}
